class Member {
	constructor (group){
		// 小组源数据
		this.group = group
		// 保存所有成员的一维数组
		this.allMemberList = []

		this._addRequireAttr(this.group)
		this._formatMember(this.group)

	}
	/**
	 * [addRequireAttr 给数据结构添加必要的参数 path 和 is_selected]
	 * @param {[Array]} group   [小组]
	 * @param {[Array]} pathArr [小组路径]
	 */
	_addRequireAttr (group, pathArr){
		group.forEach((o, i, a) => {
			group[i].path = pathArr 
                            ? pathArr.concat({name: group[i].name, id: group[i].id}) 
                            : [{name: '全部', id: -1}, {name: group[i].name, id: group[i].id}]
            // group[i].is_selected = 0
            // group[i].memberList.forEach((oo, ii, aa) => {
            // 	group[i].memberList[ii].is_selected = 0
            // })
			if(o.children.length > 0){
				this._addRequireAttr(o.children, group[i].path)
			}
		})
	}

	/**
	 * [formatMember 抽取所有小组成员为一维数组]
	 * @param  {[Array]} group [小组源数据]
	 */
	_formatMember (group){
		group.forEach((o, i, a) => {
			this.allMemberList = this.allMemberList.concat(o.memberList)
			if(o.children.length > 0){
				this._formatMember(o.children)
			}
		})
	}

	/**
	 * [findGroup 根据小组Id查找小组树]
	 * @param  {[String]} groupId [小组名字]
	 * @return {[Array]}           [符合条件的数组]
	 */
	findGroupTreeById (groupId){
		let groupTree
		let finder = (group) => {
        	group.some((o, i, a) => {
				if(o.id == groupId){
	        		groupTree = o
	        		return true
	        	}
	        	if(o.children.length > 0){
					finder(o.children)
				}
	        })
        }
        finder(this.group)
        return groupTree
	}

	/**
	 * [selectGroupById 选中/移除 一个小组的成员，该方法会自动 选中/移除 子小组的成员，并检查当前小组同级的小组是否全选，来动态的把父级 选中/移除]
	 * @param  {[Number]} groupId [小组id]
	 * @return {[type]}         [description]
	 */
	setMemberByGroupId (groupId, label){
        // 将该组 和 该组的成员 的 is_selected 设为 true
        let setCurrentGroup = (group, groupId, label) => {
        	group.some((o, i, a) => {
				if(o.id == groupId){
	        		group[i].is_selected = label
	        		group[i].memberList.forEach((oo, ii, aa) => {
	        			group[i].memberList[ii].is_selected = label
	        		})
	        		return true
	        	}
	        	if(o.children.length > 0){
					setCurrentGroup(o.children, groupId, label)
				}
	        })
        }
        setCurrentGroup(this.group, groupId, label)

        // 将该组的子小组 和 子小组成员 的 is_selected 设为 true
        let childTree = this.findGroupTreeById(groupId)
        let setChildGroup = (group) => {
        	group.forEach((o, i, a) => {
        		group[i].is_selected = label
        		group[i].memberList.forEach((oo, ii, aa) => {
        			group[i].memberList[ii].is_selected = label
        		})
        		if(o.children.length > 0){
	        		setChildGroup(o.children)
	        	}
        	})
        }
        setChildGroup([childTree])

        // 判断同级别的小组 和 成员 的 is_selected 是否都为true , 递归 将父级设为 true
        let checkerParent = (groupId) => {
        	if(groupId != 0){
	        	let parentTree = this.findGroupTreeById(groupId)
		        let isAllGroupSelect = parentTree.children.every((o, i, a) => {
		        	return parseInt(o.is_selected) ? true : false
		        })
		        let isAllMemberSelect
		        if(parentTree.memberList.length == 0){
		        	isAllMemberSelect = true
		        }else{
			        isAllMemberSelect = parentTree.memberList.every((o, i, a) => {
			        	return parseInt(o.is_selected) ? true : false
			        })
		        }

		        if(isAllGroupSelect && isAllMemberSelect){
		        	console.log(parentTree.id)
		        	// setCurrentGroup(this.group, parentTree.id, 1)
		        	parentTree.is_selected = 1
		        }else{
		        	// setCurrentGroup(this.group, parentTree.id, 0)
		        	parentTree.is_selected = 0
		        }
		        checkerParent(parentTree.pid)
	        }
        }
        checkerParent(childTree.pid)
        
        
    }

    setMemberByMemberId (memberId, label){
    	// 将 该id 的成员 的 is_selected 设置为true
    	let groupId
    	let setCurrentMember = (group) => {
        	group.forEach((o, i, a) => {
				group[i].memberList.some((oo, ii, aa) => {
					if(oo.user_id == memberId){
						groupId = oo.group_id
						group[i].memberList[ii].is_selected = label
						return true
					}
        		})
	        	if(o.children.length > 0){
					setCurrentMember(o.children)
				}
	        })
        }
        setCurrentMember(this.group)

        let checkerUp = (groupId) => {
        	let groupTree = this.findGroupTreeById(groupId)
	        let isSelectAllMember = groupTree.memberList.every((o, i, a) => {
	        	console.log(parseInt(o.is_selected))
	        	return parseInt(o.is_selected) ? true : false
	        })
	        let isSelectAllGroup
	        if(groupTree.children.length == 0){
	        	isSelectAllGroup = true
	        }else{
	        	isSelectAllGroup = groupTree.children.every((o, i, a) => {
		        	return parseInt(o.is_selected) ? true : false
		        })
	        }
	        if(isSelectAllMember && isSelectAllGroup){
        		groupTree.is_selected = 1
	        }else{
	        	groupTree.is_selected = 0
	        }
	        if(groupTree.pid != 0){	
		        let parentTree = this.findGroupTreeById(groupTree.pid)
		        checkerUp(parentTree.id)
	        }
        }
        checkerUp(groupId)

    }

    getSelectedGroup (){
    	let groups = []
    	let finder = (group) => {
    		group.forEach((o, i, a) => {
    			if(o.is_selected){
    				groups.push(o.id+'')
    			}
    			if(o.children.length > 0){
    				finder(o.children)
    			}
    		})
    	}
    	finder(this.group)
    	return groups
    }

    getSelectedMember (){
    	let members = []
    	let finder = (group) => {
    		group.forEach((o, i, a) => {
    			group[i].memberList.forEach((oo, ii, aa) => {
    				if(group[i].memberList[ii].is_selected){
    					members.push(group[i].memberList[ii].user_id+'')
    				}
    			})
    			if(o.children.length > 0){
    				finder(o.children)
    			}
    		})
    	}
    	finder(this.group)
    	return members
    }

}

export default Member